import { api } from '@/axios/instance';
import { toast } from 'sonner';

export type GetTaskConfig = AxiosRequestConfig<Pick<Task, 'id'>>;

export const getTask = async ({
  requestConfig,
  params
}: {
  requestConfig?: GetTaskConfig;
  params: any;
}) =>
  api
    .post<TaskBoardResponse>('/api/TaskBoard/get', params, requestConfig?.config)
    .then((response) => response.data.data.columns);

export type PostCreateTaskConfig = AxiosRequestConfig<Omit<PostCreateTaskParams, 'text'>>;

export const postGetTask = async ({ params, config }: GetTaskConfig) =>
  api.post<{ data: Task }>('/api/TaskBoard/getTask', params, config).then((res) => res.data.data);

export const postCreateTask = async ({ params, config }: PostCreateTaskConfig) =>
  api.post<Task>('/api/TaskBoard/taskCreate', params, config).then(responseSuccess);

export type PostCreateSubTaskConfig = AxiosRequestConfig<
  Pick<Task, 'name' | 'parent_id'> & { text?: string; project_id: string }
>;

export const postCreateSubTask = async ({ params, config }: PostCreateSubTaskConfig) =>
  api.post<Task>('/api/TaskBoard/subtaskCreate', params, config).then(responseSuccess);

type DeleteTaskConfig = AxiosRequestConfig<Pick<Task, 'id'>>;

export const postDeleteTask = async ({ params, config }: DeleteTaskConfig) =>
  api.post('/api/TaskBoard/deleteTask', params, config).then(responseSuccess);

const responseSuccess = (response: any) => {
  if (response.data.success === 'ok') {
    toast.success('Task Created 👌🏼');
    return response.data.data;
  }
};

export type PostMoveTaskConfig = AxiosRequestConfig<MoveTask>;

export const postMoveTask = async ({ params, config }: PostMoveTaskConfig) => {
  api.post('/api/TaskBoard/taskMove', params, config);
};

type StatusTaskConfig = AxiosRequestConfig<{
  id: number;
  status: number;
}>;
export const postStatusTask = async ({ params: { id, status }, config }: StatusTaskConfig) => {
  api.post<PostColumnResponse>('/api/TaskBoard/taskStatus', { id, status }, config);
};

type PinTaskConfig = AxiosRequestConfig<{
  id: number;
}>;
export const postPinTask = async ({ params: { id }, config }: PinTaskConfig) => {
  api.post('/api/TaskBoard/task/pin', { id }, config);
};
export const postUnpinTask = async ({ params: { id }, config }: PinTaskConfig) => {
  api.post('/api/TaskBoard/task/unpin', { id }, config);
};

type PriorityTaskConfig = AxiosRequestConfig<{
  id: number;
  priority_id: number;
}>;
export const postPriorityTask = async ({
  params: { id, priority_id },
  config
}: PriorityTaskConfig) => {
  api.post('/api/TaskBoard/setPriority', { id, priority_id }, config);
};

type CommentTaskConfig = AxiosRequestConfig<{
  task_id: number;
  text: string;
  files?: string;
  reply_to?: string;
}>;

export const postCreateComment = async ({ params, config }: CommentTaskConfig) => {
  api.post<PostColumnResponse>('/api/TaskBoard/comment/add', params, config);
};

type ChangeTaskConfig = AxiosRequestConfig<{
  task_id: number;
  end_date: Date | string;
}>;
export const postChangeTaskDate = async ({ params, config }: ChangeTaskConfig) => {
  api.post('/api/TaskBoard/task/setDeadline', params, config);
};
type RenameTaskConfig = AxiosRequestConfig<{
  name: string;
  task_id: number;
}>;

export const axiosRenameTask = async ({ params, config }: RenameTaskConfig) => {
  api.post('/api/TaskBoard/task/save', params, config);
};
